package grape.common.rest.security;

import grape.common.AbstractLoginUser;
import grape.common.service.loginuser.LoginUser;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * controller中注入当前登录用户参数
 * Created by yangwei
 * Created at 2020/4/9 12:57
 */
public class LoginUserArgumentResolver implements HandlerMethodArgumentResolver {

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        boolean isHasLoginUserParameter = parameter.getParameterType().isAssignableFrom(LoginUser.class);

        return isHasLoginUserParameter;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        return AbstractLoginUser.getLoginUser();
    }
}
